package org.sfx.geometry

import javafx.geometry.Bounds
import javafx.scene.Node
import javafx.scene.shape.Rectangle
import javafx.scene.Scene
import javafx.stage.Window

import org.sfx.rich.Enrich._

case class Bounds2D(minX: Double, minY: Double, maxX: Double, maxY: Double) {
  def width = maxX - minX
  def height = maxY - minY
  
  def inflate(border: Double): Bounds2D = Bounds2D(
      minX - border, minY - border, maxX + border, maxY + border)

  def translate(point: Point2D): Bounds2D = Bounds2D(
      minX + point.x, minY + point.y, maxX + point.x, maxY + point.y)
  
  def -(that: Bounds2D): Bounds2D = Bounds2D(this.minX - that.minX, this.minY - that.minY, this.maxX - that.maxX, this.maxY - that.maxY)
  
  def +(that: Bounds2D): Bounds2D = Bounds2D(this.minX + that.minX, this.minY + that.minY, this.maxX + that.maxX, this.maxY + that.maxY)
  
  def +(point: Point2D): Bounds2D = Bounds2D(this.minX + point.x, this.minY + point.y, this.maxX + point.x, this.maxY + point.y)
  
  def -(point: Point2D): Bounds2D = Bounds2D(this.minX - point.x, this.minY - point.y, this.maxX - point.x, this.maxY - point.y)
  
  def *(factor: Double): Bounds2D = Bounds2D(this.minX * factor, this.minY * factor, this.maxX * factor, this.maxY * factor)
  
  def /(factor: Double): Bounds2D = Bounds2D(this.minX / factor, this.minY / factor, this.maxX / factor, this.maxY / factor)
}

object Bounds2D {
  def apply(minPoint: Point2D, dimensions: Dimensions2D): Bounds2D = {
    Bounds2D(minPoint.x, minPoint.y, minPoint.x + dimensions.width, minPoint.y + dimensions.height)
  }
  
  def apply(fxBounds: Bounds): Bounds2D = {
    Bounds2D(fxBounds.getMinX(), fxBounds.getMinY(), fxBounds.getMaxX(), fxBounds.getMaxY())
  }
}
